//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.cloud.netflix.eureka.serviceregistry;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import java.util.HashMap;
import java.util.function.Consumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;

public class EurekaServiceRegistry implements ServiceRegistry<EurekaRegistration> {
    private static final Log log = LogFactory.getLog(EurekaServiceRegistry.class);

    public EurekaServiceRegistry() {
    }

    public void register(EurekaRegistration reg) {
        this.maybeInitializeClient(reg);
        if (log.isInfoEnabled()) {
            log.info("Registering application " + reg.getApplicationInfoManager().getInfo().getAppName() + " with eureka with status " + reg.getInstanceConfig().getInitialStatus());
        }

        reg.getApplicationInfoManager().setInstanceStatus(reg.getInstanceConfig().getInitialStatus());
        reg.getHealthCheckHandler().ifAvailable((healthCheckHandler) -> {
            reg.getEurekaClient().registerHealthCheck(healthCheckHandler);
        });
    }

    private void maybeInitializeClient(EurekaRegistration reg) {
        reg.getApplicationInfoManager().getInfo();
        reg.getEurekaClient().getApplications();
    }

    public void deregister(EurekaRegistration reg) {
        if (reg.getApplicationInfoManager().getInfo() != null) {
            if (log.isInfoEnabled()) {
                log.info("Unregistering application " + reg.getApplicationInfoManager().getInfo().getAppName() + " with eureka with status DOWN");
            }

            reg.getApplicationInfoManager().setInstanceStatus(InstanceStatus.DOWN);
        }

    }

    public void setStatus(EurekaRegistration registration, String status) {
        InstanceInfo info = registration.getApplicationInfoManager().getInfo();
        if ("CANCEL_OVERRIDE".equalsIgnoreCase(status)) {
            registration.getEurekaClient().cancelOverrideStatus(info);
        } else {
            InstanceStatus newStatus = InstanceStatus.toEnum(status);
            registration.getEurekaClient().setStatus(newStatus, info);
        }
    }

    public Object getStatus(EurekaRegistration registration) {
        String appname = registration.getApplicationInfoManager().getInfo().getAppName();
        String instanceId = registration.getApplicationInfoManager().getInfo().getId();
        InstanceInfo info = registration.getEurekaClient().getInstanceInfo(appname, instanceId);
        HashMap<String, Object> status = new HashMap();
        if (info != null) {
            status.put("status", info.getStatus().toString());
            status.put("overriddenStatus", info.getOverriddenStatus().toString());
        } else {
            status.put("status", InstanceStatus.UNKNOWN.toString());
        }

        return status;
    }

    public void close() {
    }
}
